{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 6 Pandas的函数应用"
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# Numpy ufunc 函数，randn跟的是维数\n",
    "df = pd.DataFrame(np.random.randn(5,4))\n",
    "print(df)\n",
    "\n",
    "print(np.abs(df)) #绝对值"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:28.124127Z",
     "start_time": "2025-09-06T10:28:28.115250Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -1.608756  0.275180 -0.025266 -0.935850\n",
      "1 -1.108562  2.360810  0.708617 -1.636763\n",
      "2  0.331455 -1.398914 -0.521208 -0.684086\n",
      "3  0.537818  0.043571  1.217464 -0.637933\n",
      "4 -1.131136  0.609582 -0.586249  0.731842\n",
      "          0         1         2         3\n",
      "0  1.608756  0.275180  0.025266  0.935850\n",
      "1  1.108562  2.360810  0.708617  1.636763\n",
      "2  0.331455  1.398914  0.521208  0.684086\n",
      "3  0.537818  0.043571  1.217464  0.637933\n",
      "4  1.131136  0.609582  0.586249  0.731842\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "source": [
    "#apply默认作用在列上,x是每一列,因为axis=0\n",
    "print(df.apply(lambda x : x.max()))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:28.221337Z",
     "start_time": "2025-09-06T10:28:28.215075Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.537818\n",
      "1    2.360810\n",
      "2    1.217464\n",
      "3    0.731842\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "source": [
    "#apply作用在行上\n",
    "print(df.apply(lambda x : x.max(), axis=1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:28.312501Z",
     "start_time": "2025-09-06T10:28:28.307907Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.275180\n",
      "1    2.360810\n",
      "2    0.331455\n",
      "3    1.217464\n",
      "4    0.731842\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "source": [
    "# 使用map应用到每个数据\n",
    "print(df.map(lambda x : '%.2f' % x))\n",
    "df.dtypes"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:28.402541Z",
     "start_time": "2025-09-06T10:28:28.392393Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2      3\n",
      "0  -1.61   0.28  -0.03  -0.94\n",
      "1  -1.11   2.36   0.71  -1.64\n",
      "2   0.33  -1.40  -0.52  -0.68\n",
      "3   0.54   0.04   1.22  -0.64\n",
      "4  -1.13   0.61  -0.59   0.73\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0    float64\n",
       "1    float64\n",
       "2    float64\n",
       "3    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "source": [
    "type('%.2f' % 1.3456)\n",
    "print('%.2f' % 1.3456)\n",
    "print(np.round(1.3456,2))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:28.511793Z",
     "start_time": "2025-09-06T10:28:28.506791Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.35\n",
      "1.35\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "cell_type": "markdown",
   "source": "## 6.4 索引排序（不重要）",
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Series\n",
    "print(np.random.randint(5, size=5))\n",
    "print('-'*50)\n",
    "s4 = pd.Series(range(10, 15), index = np.random.randint(5, size=5)) #索引随机生成\n",
    "print(s4)\n",
    "print('-'*50)\n",
    "# 索引排序,sort_index返回一个新的排好索引的series\n",
    "print(s4.sort_index())\n",
    "print(s4)\n",
    "# s4.loc[0:3]  loc索引值不唯一时直接报错\n",
    "print(s4.iloc[0:3])\n",
    "s4[0:3]  #默认用的位置索引"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:28.709389Z",
     "start_time": "2025-09-06T10:28:28.694721Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 1 3 0 2]\n",
      "--------------------------------------------------\n",
      "3    10\n",
      "3    11\n",
      "4    12\n",
      "0    13\n",
      "3    14\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "0    13\n",
      "3    10\n",
      "3    11\n",
      "3    14\n",
      "4    12\n",
      "dtype: int64\n",
      "3    10\n",
      "3    11\n",
      "4    12\n",
      "0    13\n",
      "3    14\n",
      "dtype: int64\n",
      "3    10\n",
      "3    11\n",
      "4    12\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3    10\n",
       "3    11\n",
       "4    12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "source": [
    "# s4.loc[1:2] #loc索引值唯一时可以切片"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:28.879454Z",
     "start_time": "2025-09-06T10:28:28.875446Z"
    }
   },
   "outputs": [],
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "source": [
    "# DataFrame\n",
    "df4 = pd.DataFrame(np.random.randn(5, 5),\n",
    "                   index=np.random.randint(5, size=5),\n",
    "                   columns=np.random.randint(5, size=5))\n",
    "print(df4)\n",
    "#轴零是行索引排序\n",
    "df4_isort = df4.sort_index(axis=0, ascending=False)\n",
    "print(df4_isort)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:29.026857Z",
     "start_time": "2025-09-06T10:28:29.015616Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1         2         4         3         4\n",
      "1 -0.829077  1.077649  0.301559  1.005215 -0.476300\n",
      "4 -2.623294 -0.242003  0.778198  1.400962 -0.065715\n",
      "4 -0.962044 -1.487587  0.343872  1.468642  0.215973\n",
      "2 -2.510459  1.252101  0.189384 -0.113566 -0.241454\n",
      "2  0.490841  0.545878  0.716664  0.533837  1.006531\n",
      "          1         2         4         3         4\n",
      "4 -2.623294 -0.242003  0.778198  1.400962 -0.065715\n",
      "4 -0.962044 -1.487587  0.343872  1.468642  0.215973\n",
      "2 -2.510459  1.252101  0.189384 -0.113566 -0.241454\n",
      "2  0.490841  0.545878  0.716664  0.533837  1.006531\n",
      "1 -0.829077  1.077649  0.301559  1.005215 -0.476300\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "source": [
    "#轴1是列索引排序\n",
    "df4_isort = df4.sort_index(axis=1, ascending=True)\n",
    "print(df4_isort)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:29.165809Z",
     "start_time": "2025-09-06T10:28:29.158503Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1         2         3         4         4\n",
      "1 -0.829077  1.077649  1.005215  0.301559 -0.476300\n",
      "4 -2.623294 -0.242003  1.400962  0.778198 -0.065715\n",
      "4 -0.962044 -1.487587  1.468642  0.343872  0.215973\n",
      "2 -2.510459  1.252101 -0.113566  0.189384 -0.241454\n",
      "2  0.490841  0.545878  0.533837  0.716664  1.006531\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "cell_type": "markdown",
   "source": "# 6.5 按值排序（机器学习，深度学习不重要，数据分析才需要）",
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# 按值排序,by后是column的值\n",
    "import random\n",
    "l=[random.randint(0,100) for i in range(24)] #生成24个随机数\n",
    "df4 = pd.DataFrame(np.array(l).reshape(6,4)) #生成6行4列的dataframe\n",
    "# print(df4) #查看数据,ndarray\n",
    "# print('-'*50)\n",
    "print(df4)\n",
    "print('-'*50)\n",
    "#按轴零排序，by后是列名,交换的是行\n",
    "df4_vsort = df4.sort_values(by=3,axis=0, ascending=False) #寻找的是columns里的3,重要\n",
    "print(df4_vsort)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:29.278493Z",
     "start_time": "2025-09-06T10:28:29.268200Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1   2   3\n",
      "0  27  77  42  46\n",
      "1  46  23  21  40\n",
      "2  91   9  41  24\n",
      "3  67  91  68  30\n",
      "4  28  93  86  17\n",
      "5   3  70  56  55\n",
      "--------------------------------------------------\n",
      "    0   1   2   3\n",
      "5   3  70  56  55\n",
      "0  27  77  42  46\n",
      "1  46  23  21  40\n",
      "3  67  91  68  30\n",
      "2  91   9  41  24\n",
      "4  28  93  86  17\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "source": [
    "#按轴1排序，by后行索引名，交换的是列\n",
    "df4_vsort = df4.sort_values(by=3,axis=1, ascending=False) #寻找的是index里的3\n",
    "print(df4_vsort)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:29.403295Z",
     "start_time": "2025-09-06T10:28:29.398057Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    1   2   0   3\n",
      "0  77  42  27  46\n",
      "1  23  21  46  40\n",
      "2   9  41  91  24\n",
      "3  91  68  67  30\n",
      "4  93  86  28  17\n",
      "5  70  56   3  55\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "cell_type": "markdown",
   "source": "# 6.6 处理缺失数据（重要）",
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan],\n",
    "                       [np.nan, 4., np.nan], [1., 2., 3.]])\n",
    "# df_data3=pd.DataFrame([np.random.randn(3), [1., 2., np.nan],[np.nan, 4., np.nan], [1., 2., 3.]])\n",
    "# df_data2=pd.DataFrame(np.arange(12).reshape(3,4).astype(np.float64))\n",
    "# df_data2[1,2]=np.nan  #dataframe如何访问元素\n",
    "# print(df_data2)\n",
    "print(df_data.head())\n",
    "print(df_data.isnull())\n",
    "df_data.iloc[1,2]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:29.521501Z",
     "start_time": "2025-09-06T10:28:29.509054Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0        1         2\n",
      "0  0.383617 -0.57978  0.737718\n",
      "1  1.000000  2.00000       NaN\n",
      "2       NaN  4.00000       NaN\n",
      "3  1.000000  2.00000  3.000000\n",
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.float64(nan)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "source": [
    "print(df_data.iloc[2,0])\n",
    "print(df_data.loc[2,2])\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:29.618876Z",
     "start_time": "2025-09-06T10:28:29.614805Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan\n",
      "nan\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "source": [
    "#isnull来判断是否有空的数据\n",
    "print(df_data.isnull())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:29.710805Z",
     "start_time": "2025-09-06T10:28:29.704192Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:29.840055Z",
     "start_time": "2025-09-06T10:28:29.827610Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#帮我计算df_data缺失率\n",
    "ser=pd.Series(np.arange(10))\n",
    "ser=ser+3  #广播运算\n",
    "print(ser)\n",
    "print(df_data)\n",
    "len(df_data)\n",
    "print(len(df_data))\n",
    "print(df_data.isnull().sum())\n",
    "# print(df_data)\n",
    "print(df_data.isnull().sum()/len(df_data))#符合广播运算\n",
    "#返回一列中的最大值\n",
    "print(df_data.max(axis=0))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     3\n",
      "1     4\n",
      "2     5\n",
      "3     6\n",
      "4     7\n",
      "5     8\n",
      "6     9\n",
      "7    10\n",
      "8    11\n",
      "9    12\n",
      "dtype: int64\n",
      "          0        1         2\n",
      "0  0.383617 -0.57978  0.737718\n",
      "1  1.000000  2.00000       NaN\n",
      "2       NaN  4.00000       NaN\n",
      "3  1.000000  2.00000  3.000000\n",
      "4\n",
      "0    1\n",
      "1    0\n",
      "2    2\n",
      "dtype: int64\n",
      "0    0.25\n",
      "1    0.00\n",
      "2    0.50\n",
      "dtype: float64\n",
      "0    1.0\n",
      "1    4.0\n",
      "2    3.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 6.7 处理重复数据（不重要）"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:29.898922Z",
     "start_time": "2025-09-06T10:28:29.895722Z"
    }
   },
   "cell_type": "code",
   "source": "# 重复数据，重复的行，重复的列",
   "outputs": [],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 6.8 合并数据（重要）"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:29.977107Z",
     "start_time": "2025-09-06T10:28:29.973277Z"
    }
   },
   "cell_type": "code",
   "source": "# 合并数据，concat,join,merge",
   "outputs": [],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 6.9 重塑数据（不重要）"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:30.021722Z",
     "start_time": "2025-09-06T10:28:30.018566Z"
    }
   },
   "cell_type": "code",
   "source": "# 重塑数据，pivot,stack,unstack",
   "outputs": [],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 6.10 统计数据（重要）"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:30.082694Z",
     "start_time": "2025-09-06T10:28:30.079946Z"
    }
   },
   "cell_type": "code",
   "source": "# 统计数据，describe,corr,cov,mean,median,std,var,skew,kurt,quantile,cumsum,cumprod,rank,nunique,unique,value_counts",
   "outputs": [],
   "execution_count": 29
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 6.11 绘图数据（重要）"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:30.130326Z",
     "start_time": "2025-09-06T10:28:30.128688Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 删除缺失数据"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "#默认一个样本，任何一个特征缺失，就删除\n",
    "#inplace True是修改的是原有的df\n",
    "#subset=[0]是指按第一列来删除,第一列有空值就删除对应的行\n",
    "print(df_data)\n",
    "# print(df_data.dropna(inplace=True))#默认删除所有空值对应的行\n",
    "# print(df_data.dropna(subset=[0],inplace=True))\n",
    "df_data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:30.212044Z",
     "start_time": "2025-09-06T10:28:30.202640Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0        1         2\n",
      "0  0.383617 -0.57978  0.737718\n",
      "1  1.000000  2.00000       NaN\n",
      "2       NaN  4.00000       NaN\n",
      "3  1.000000  2.00000  3.000000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "          0        1         2\n",
       "0  0.383617 -0.57978  0.737718\n",
       "1  1.000000  2.00000       NaN\n",
       "2       NaN  4.00000       NaN\n",
       "3  1.000000  2.00000  3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.383617</td>\n",
       "      <td>-0.57978</td>\n",
       "      <td>0.737718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.00000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "cell_type": "code",
   "source": [
    "df_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:30.328500Z",
     "start_time": "2025-09-06T10:28:30.321787Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          0        1         2\n",
       "0  0.383617 -0.57978  0.737718\n",
       "1  1.000000  2.00000       NaN\n",
       "2       NaN  4.00000       NaN\n",
       "3  1.000000  2.00000  3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.383617</td>\n",
       "      <td>-0.57978</td>\n",
       "      <td>0.737718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.00000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 31
  },
  {
   "cell_type": "code",
   "source": [
    "#用的不多，用在某个特征缺失太多时，才会进行删除\n",
    "print(df_data.dropna(axis=1))  #某列由nan就删除该列"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T02:17:31.138960Z",
     "start_time": "2025-09-06T02:17:31.132595Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1\n",
      "0 -0.279448\n",
      "1  2.000000\n",
      "2  4.000000\n",
      "3  2.000000\n"
     ]
    }
   ],
   "execution_count": 63
  },
  {
   "cell_type": "code",
   "source": [
    "df_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:33.418970Z",
     "start_time": "2025-09-06T10:28:33.409625Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          0        1         2\n",
       "0  0.383617 -0.57978  0.737718\n",
       "1  1.000000  2.00000       NaN\n",
       "2       NaN  4.00000       NaN\n",
       "3  1.000000  2.00000  3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.383617</td>\n",
       "      <td>-0.57978</td>\n",
       "      <td>0.737718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.00000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 32
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 填充缺失数据"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:38.906255Z",
     "start_time": "2025-09-06T10:28:38.903602Z"
    }
   },
   "cell_type": "code",
   "source": "#均值，中位数，众数填充",
   "outputs": [],
   "execution_count": 33
  },
  {
   "cell_type": "code",
   "source": [
    "#给零列的空值填为-100，按特征（按列）去填充\n",
    "df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan],\n",
    "                       [np.nan, 4., np.nan], [1., 2., 3.]])\n",
    "print(df_data)\n",
    "print(df_data.iloc[:,0].fillna(-100.))\n",
    "# 缺失最多的列\n",
    "idx_max=df_data.isnull().sum().idxmax()\n",
    "print(idx_max)\n",
    "# print(df_data.iloc[:,idx_max].fillna(-100.))\n",
    "# print(df_data)\n",
    "df_data.iloc[:,idx_max]=df_data.iloc[:,idx_max].fillna(-100.)#尝试进行按列赋值结果失败了,这里是没有用iloc导致\n",
    "print(df_data)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T02:31:33.603793Z",
     "start_time": "2025-09-06T02:31:33.595390Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0  0.072802 -1.011761  2.389802\n",
      "1  1.000000  2.000000       NaN\n",
      "2       NaN  4.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n",
      "0      0.072802\n",
      "1      1.000000\n",
      "2   -100.000000\n",
      "3      1.000000\n",
      "Name: 0, dtype: float64\n",
      "2\n",
      "          0         1           2\n",
      "0  0.072802 -1.011761    2.389802\n",
      "1  1.000000  2.000000 -100.000000\n",
      "2       NaN  4.000000 -100.000000\n",
      "3  1.000000  2.000000    3.000000\n"
     ]
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:47.466092Z",
     "start_time": "2025-09-06T10:28:47.440882Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_data.iloc[:,idx_max].fillna(-100.,inplace=True))",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'idx_max' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[34]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m \u001B[38;5;28mprint\u001B[39m(df_data.iloc[:,\u001B[43midx_max\u001B[49m].fillna(-\u001B[32m100.\u001B[39m,inplace=\u001B[38;5;28;01mTrue\u001B[39;00m))\n",
      "\u001B[31mNameError\u001B[39m: name 'idx_max' is not defined"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:52.013684Z",
     "start_time": "2025-09-06T10:28:52.005649Z"
    }
   },
   "cell_type": "code",
   "source": "df_data\n",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          0        1         2\n",
       "0  0.383617 -0.57978  0.737718\n",
       "1  1.000000  2.00000       NaN\n",
       "2       NaN  4.00000       NaN\n",
       "3  1.000000  2.00000  3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.383617</td>\n",
       "      <td>-0.57978</td>\n",
       "      <td>0.737718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.00000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "source": [
    "#依次拿到每一列\n",
    "for i in df_data.columns:\n",
    "    print(df_data.loc[:,i])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-09-06T10:28:56.600573Z",
     "start_time": "2025-09-06T10:28:56.594938Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.383617\n",
      "1    1.000000\n",
      "2         NaN\n",
      "3    1.000000\n",
      "Name: 0, dtype: float64\n",
      "0   -0.57978\n",
      "1    2.00000\n",
      "2    4.00000\n",
      "3    2.00000\n",
      "Name: 1, dtype: float64\n",
      "0    0.737718\n",
      "1         NaN\n",
      "2         NaN\n",
      "3    3.000000\n",
      "Name: 2, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:35:30.919530Z",
     "start_time": "2025-01-07T07:35:30.916262Z"
    }
   },
   "cell_type": "code",
   "source": "df_data.iloc[:,0].fillna(-100.,inplace=True) #inplace=True后面会被删除",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\41507\\AppData\\Local\\Temp\\ipykernel_10424\\3395572301.py:1: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  df_data.iloc[:,0].fillna(-100.,inplace=True)\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:37:49.794275Z",
     "start_time": "2025-01-07T07:37:49.791340Z"
    }
   },
   "cell_type": "code",
   "source": "df_data.iloc[:,2]=df_data.iloc[:,2].fillna(df_data.iloc[:,2].mean()) #用均值填充空值",
   "outputs": [],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:37:55.580904Z",
     "start_time": "2025-01-07T07:37:55.575901Z"
    }
   },
   "cell_type": "code",
   "source": "df_data",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            0         1         2\n",
       "0   -1.264229 -0.514817  1.420473\n",
       "1    1.000000  2.000000  2.210236\n",
       "2 -100.000000  4.000000  2.210236\n",
       "3    1.000000  2.000000  3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.264229</td>\n",
       "      <td>-0.514817</td>\n",
       "      <td>1.420473</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>2.210236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-100.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>2.210236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
